package com.hzsoft.sys.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hzsoft.base.entity.BJuiJSON;
import com.hzsoft.base.entity.Page;
import com.hzsoft.sys.entity.SysMenu;
import com.hzsoft.sys.service.SysMenuService;
import com.hzsoft.util.StrUtil;

@Controller
@RequestMapping("/system")
public class SysMenuController extends com.hzsoft.base.BaseController{
	
	@Autowired
	SysMenuService menuService;
	
	@RequestMapping("/systemMenuList")
	public ModelAndView listMenu(@ModelAttribute SysMenu menu,Model model,HttpServletRequest request,HttpServletResponse response){
		//分页设置
		String pageCurrent = request.getParameter("pageCurrent");
		Page page =  new Page();
		if (StringUtils.isNotBlank(pageCurrent)) {
			page.setPageCurrent(Integer.parseInt(pageCurrent));
			menu.setPage(page);
		}
		
		if(menu.getName() == null){
			menu.setName("");
		}
		menu.setName(menu.getName().trim());
		List<SysMenu> list = menuService.listPageMenu(menu);
		
		model.addAttribute("menu",menu);
		model.addAttribute("page", menu.getPage());
		model.addAttribute("list",list);
		return new ModelAndView("system/menu/menuList");
	}
	
	/**
	 * 弹出添加页面
	 * @param menu
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping("/prepareAddMenu")
	public ModelAndView prepareAdd(@ModelAttribute SysMenu menu,HttpServletResponse response, HttpServletRequest request){
		ModelAndView model = new ModelAndView();
	
		model.setViewName("system/menu/addMenu");
		return model;
	}
	
	
	/**
	 * 功能设置：添加
	 */
	@RequestMapping(value = "/addMenu")	
	public ModelAndView addMenu(SysMenu record,HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		BJuiJSON bjui = new BJuiJSON();
		List<SysMenu> list = menuService.listPageMenu(record);
		if(list!=null&&list.size()>0){
			bjui.setStatusCode("300");
			bjui.setMessage("该项已存在！！！");
		}else{
			try {
				menuService.insertSelective(record);
				
				bjui.setStatusCode("200");
				bjui.setMessage("添加成功！！！");
				bjui.setTabid("navtab-menu");
				bjui.setCloseCurrent(true);
				
			} catch (Exception e) {
				e.printStackTrace();				
				bjui.setStatusCode("300");
				bjui.setMessage("添加失败！！！");
			}
		}
		String msg = JSONObject.fromObject(bjui).toString();
		mv.addObject("msg", msg);
		mv.setViewName("msg");
		return mv;
	}
	
	/**
	 * 功能设置：删除
	 */
	@RequestMapping(value = "/deleteMenu")
	public ModelAndView deleteMenu(@ModelAttribute SysMenu menu,HttpServletRequest request,HttpServletResponse response) throws IOException{
		BJuiJSON bjui = new BJuiJSON();
		ModelAndView mv = new ModelAndView();
		
		String delids = request.getParameter("delids");
		
		String [] ids= StrUtil.funsplit(delids);//转成数组
		
		menu.setIds(ids);
		
		try {
			menuService.deleteAll(menu);
			
			bjui.setStatusCode("200");
			bjui.setMessage("删除成功！！！");
			bjui.setTabid("navtab-menu");
		} catch (Exception e) {
			e.printStackTrace();
			bjui.setStatusCode("300");
			bjui.setMessage("删除失败！！！");
		}

		String msg = JSONObject.fromObject(bjui).toString();
		mv.addObject("msg",msg);
		mv.setViewName("msg");
		return mv;
	}
	
	/**
	 * 弹出Menu修改页面
	 * @param 
	 * @return
	 */
	@RequestMapping(value="/toUpdateMenu")
	public ModelAndView toUpdateMenu(SysMenu menu){
		ModelAndView mv = new ModelAndView();
		System.out.println("-----------"+menu.getId());
		mv.addObject("menu", menuService.selectByPrimaryKey(menu.getId()));
//		mv.addObject("menu", menuService.selectMenu(menu.getpMenuid()));
		mv.setViewName("system/menu/updateMenu");
		return mv;
	}
	
	/**
	 * Menu:修改Menu功能信息
	 * @param menu
	 * @return
	 */
	@RequestMapping(value="/updateMenu",method=RequestMethod.POST)
	public ModelAndView updateMenu(SysMenu menu,HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		BJuiJSON bjui = new BJuiJSON();
		try {
			menuService.updateByPrimaryKeySelective(menu);
			
			bjui.setStatusCode("200");
			bjui.setMessage("更新成功！！！");
			bjui.setTabid("navtab-menu");
			bjui.setCloseCurrent(true);
			
		} catch (Exception e) {
			bjui.setStatusCode("300");
			bjui.setMessage("更新失败！！！");
		}
		String msg = JSONObject.fromObject(bjui).toString();
		mv.addObject("msg",msg);
		mv.setViewName("msg");
		return mv;
	}
}